package serverapp;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

class MyCellRenderer extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value, // value to display
            int index, // cell index
            boolean isSelected, // is the cell selected
            boolean cellHasFocus) // the list and the cell have the focus
    {
        Player name = (Player) value;
        setText(name.getName() + " " + name.getSurname());
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);
        return this;
    }
}

public class ChoosePlayers extends javax.swing.JFrame {


    public ChoosePlayers() {
    }
    public ChoosePlayers(Team aTeam1, Team aTeam2) {
        initComponents();
        playersHome.setListData(ServerView.myBase.getPlayerList(aTeam1.id).toArray());
        playersAway.setListData(ServerView.myBase.getPlayerList(aTeam2.id).toArray());
        playersHome.setCellRenderer(new MyCellRenderer());
        playersAway.setCellRenderer(new MyCellRenderer());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        playersHome = new javax.swing.JList();
        homeLabel = new javax.swing.JLabel();
        awayLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        playersAway = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        playersHome.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(playersHome);

        homeLabel.setText("jLabel1");

        awayLabel.setText("jLabel2");

        playersAway.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(playersAway);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        errorLabel.setForeground(new java.awt.Color(255, 0, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 6, Short.MAX_VALUE)
                        .addGap(390, 390, 390))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(homeLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(awayLabel)))
                            .addComponent(jButton1))
                        .addContainerGap(316, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(homeLabel)
                    .addComponent(awayLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(20, 20, 20)
                .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ArrayList<Player> homePlayersList = new ArrayList();
        ArrayList<Player> awayPlayersList = new ArrayList();
        int j = 0;
        for (int i = playersHome.getMinSelectionIndex(); i <= playersHome.getMaxSelectionIndex(); i++) {
            if (playersHome.isSelectedIndex(i)) {
                Player element = (Player) playersHome.getModel().getElementAt(i);
                homePlayersList.add(element);
                j++;
            }
        }
        int k = 0;
        for (int i = playersAway.getMinSelectionIndex(); i <= playersAway.getMaxSelectionIndex(); i++) {
            if (playersAway.isSelectedIndex(i)) {
                Player element = (Player) playersAway.getModel().getElementAt(i);
                awayPlayersList.add(element);
                k++;
            }
        }
        ServerView.homePlayers = homePlayersList;
        ServerView.awayPlayers = awayPlayersList;
        if (j == 6 && k == 6) {
            ServerView.players.addAll(homePlayersList);
            ServerView.players.addAll(awayPlayersList);
            ServerView.player1HomeLabel.setText(homePlayersList.get(0).name + " " + homePlayersList.get(0).surname);
            ServerView.player2HomeLabel.setText(homePlayersList.get(1).name + " " + homePlayersList.get(1).surname);
            ServerView.player3HomeLabel.setText(homePlayersList.get(2).name + " " + homePlayersList.get(2).surname);
            ServerView.player4HomeLabel.setText(homePlayersList.get(3).name + " " + homePlayersList.get(3).surname);
            ServerView.player5HomeLabel.setText(homePlayersList.get(4).name + " " + homePlayersList.get(4).surname);
            ServerView.player6HomeLabel.setText(homePlayersList.get(5).name + " " + homePlayersList.get(5).surname);

            ServerView.player1AwayLabel.setText(awayPlayersList.get(0).name + " " + awayPlayersList.get(0).surname);
            ServerView.player2AwayLabel.setText(awayPlayersList.get(1).name + " " + awayPlayersList.get(1).surname);
            ServerView.player3AwayLabel.setText(awayPlayersList.get(2).name + " " + awayPlayersList.get(2).surname);
            ServerView.player4AwayLabel.setText(awayPlayersList.get(3).name + " " + awayPlayersList.get(3).surname);
            ServerView.player5AwayLabel.setText(awayPlayersList.get(4).name + " " + awayPlayersList.get(4).surname);
            ServerView.player6AwayLabel.setText(awayPlayersList.get(5).name + " " + awayPlayersList.get(5).surname);

            
            this.setVisible(false);
            ChooseRefreeAndHall frame = new ChooseRefreeAndHall(ServerView.countryField.getText());
            frame.setVisible(true);
        } else {
            errorLabel.setText("Nalezy wybrac dokladnie po 6 zawodnikow w druzynie!");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChoosePlayers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChoosePlayers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChoosePlayers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChoosePlayers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ChoosePlayers().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JLabel awayLabel;
    private javax.swing.JLabel errorLabel;
    public static javax.swing.JLabel homeLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList playersAway;
    private javax.swing.JList playersHome;
    // End of variables declaration//GEN-END:variables
}
